import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { Platform } from 'ionic-angular';
import { UnitsProvider } from '../units/units';
/*
  Generated class for the StorageProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class StorageProvider {

	constructor(
		public storage: Storage,
		public platform: Platform,
		public units: UnitsProvider) {
		console.log('Hello StorageProvider Provider');
	}

	/**
	 * 保存数据
	 */
	public saveStorage(name: any, value: any) {
		return new Promise((success) => {
			if(this.units.isWechat()) {
				if(value instanceof Object) {
					//存对象名字
					let keyname = '';
					for(let key in value) {
						keyname = keyname + key + ",";
					}
					keyname = keyname.substring(0, keyname.length - 1);
					sessionStorage.setItem("keys_" + name, keyname);
					//可以存对象
					for(let key in value) {
						sessionStorage.setItem(name + "_" + key, value[key]);
					}
				} else {
					sessionStorage.setItem(name, value);
				}
				if(success)
					success();
			} else if(this.platform.is('cordova')) {
				//可以存对象
				this.storage.set(name, value).then(val => {
					if(success)
						success();
				});
			} else {
				if(value instanceof Object) {
					//存对象名字
					let keyname = '';
					for(let key in value) {
						keyname = keyname + key + ",";
					}
					keyname = keyname.substring(0, keyname.length - 1);
					localStorage.setItem("keys_" + name, keyname);
					//可以存对象
					for(let key in value) {
						localStorage.setItem(name + "_" + key, value[key]);
					}
				} else {
					localStorage.setItem(name, value);
				}
				if(success)
					success();
			}
		});
	}

	/**
	 * 指定删除
	 */
	public removeStorage(name: any) {
		return new Promise((success) => {
			if(this.units.isWechat()) {
				//1.先判断取值是否取到
				let value = sessionStorage.getItem(name);
				if(value != undefined) {
					if(success)
						success(value);
				} else {
					let keyname = sessionStorage.getItem("keys_" + name);
					if(keyname != undefined) {
						let items = keyname.split(',');
						for(let i = 0; i < items.length; i++) {
							sessionStorage.removeItem(name + "_" + items[i]);
						}
						sessionStorage.removeItem("keys_" + name);
						if(success)
							success();
					} else {
						if(success)
							success();
					}
				}
			} else if(this.platform.is('cordova')) {
				this.storage.remove(name).then(val => {
					if(success)
						success();
				});
			} else {
				//1.先判断取值是否取到
				let value = localStorage.getItem(name);
				if(value != undefined) {
					if(success)
						success(value);
				} else {
					let keyname = localStorage.getItem("keys_" + name);
					if(keyname != undefined) {
						let items = keyname.split(',');
						for(let i = 0; i < items.length; i++) {
							localStorage.removeItem(name + "_" + items[i]);
						}
						localStorage.removeItem("keys_" + name);
						if(success)
							success();
					} else {
						if(success)
							success();
					}
				}
			}
		});
	}

	/**
	 * 删除所有
	 */
	public clearStorage() {
		return new Promise((success) => {
			if(this.units.isWechat()) {
				sessionStorage.clear();
				if(success)
					success();
			} else if(this.platform.is('cordova')) {
				this.storage.clear().then(val => {
					if(success)
						success();
				});
			} else {
				localStorage.clear();
				if(success)
					success();
			}
		});
	}

	/**
	 * 指定数据
	 */
	public getStorage(name: any) {
		//登录成功
		return new Promise((success) => {
			if(this.units.isWechat()) {
				//1.先判断取值是否取到
				let value = sessionStorage.getItem(name);
				if(value != undefined) {
					if(success)
						success(value);
				} else {
					let keyname = sessionStorage.getItem("keys_" + name);
					if(keyname != undefined) {
						let items = keyname.split(',');
						let val = new Object();
						for(let i = 0; i < items.length; i++) {
							val[items[i]] = sessionStorage.getItem(name + "_" + items[i]);
						}
						if(success)
							success(val);
					} else {
						if(success)
							success(undefined);
					}
				}
			} else if(this.platform.is('cordova')) {
				this.storage.get(name).then((val) => {
					if(success)
						success(val);
				});
			} else {
				//1.先判断取值是否取到
				let value = localStorage.getItem(name);
				if(value != undefined) {
					if(success)
						success(value);
				} else {
					//2.然后判断keys
					let keyname = localStorage.getItem("keys_" + name);
					if(keyname != undefined) {
						let items = keyname.split(',');
						let val = new Object();
						for(let i = 0; i < items.length; i++) {
							val[items[i]] = localStorage.getItem(name + "_" + items[i]);
						}
						if(success)
							success(val);
					} else {
						if(success)
							success(undefined);
					}
				}
			}
		});
	}
}